package com.lining.orangeservice.factory;

import com.lining.orangeservice.common.enums.OrderStatus;
import com.lining.orangeservice.common.enums.OrderType;
import com.lining.orangeservice.entity.abstractentity.Order;
import com.lining.orangeservice.entity.abstractentity.impl.GroupBuyOrder;
import com.lining.orangeservice.entity.abstractentity.impl.NormalOrder;
import com.lining.orangeservice.entity.abstractentity.impl.SeckillOrder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 订单工厂
 */
@Component
public class OrderFactory {
    
    public Order createOrder(OrderType type, Long userId) {
        Order order;
        
        switch (type) {
            case NORMAL:
                order = new NormalOrder();
                break;
            case GROUP_BUY:
                order = new GroupBuyOrder();
                break;
            case SECKILL:
                order = new SeckillOrder();
                break;
            default:
                throw new IllegalArgumentException("不支持的订单类型: " + type);
        }
        
        order.setOrderNo(generateOrderNo(type));
        order.setUserId(userId);
        order.setStatus(OrderStatus.PENDING);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        
        return order;
    }
    
    private String generateOrderNo(OrderType type) {
        String prefix;
        switch (type) {
            case NORMAL: prefix = "NO"; break;
            case GROUP_BUY: prefix = "GB"; break;
            case SECKILL: prefix = "SK"; break;
            default: prefix = "OD";
        }
        return prefix + System.currentTimeMillis();
    }
}